<?php
header('Content-Type:text/html; charset=utf-8');
require_once 'init.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();

$table = empty($_GET['table'])?'content':$_GET['table'];

$catetable = ($table == 'fanhuan_content')?'fanhuan_category':'category';

$where = "";
if (!empty($_GET['startTime'])) {
	$where .= "and p.PublishDate>='".$_GET['startTime']."' ";
}
if (!empty($_GET['endTime'])) {
	$where .= "and p.PublishDate<='".$_GET['endTime']."' ";
}
if (!empty($_GET['updater'])) {
	$where .= "and p.Updater='".$_GET['updater']."' ";
}
if (!empty($_GET['cateid'])) {
	$where .= "and p.cateID='".$_GET['cateid']."' ";
}
$db->sql="select p.*,a.nickName,c.cate from {$table} p left join admin a on p.Updater=a.userName left join {$catetable} c on p.cateID=c.id where 1=1 ".$where." AND p.`isDel`=0 order by sort asc,p.PublishDate desc";
$pictures = $db->doResultList2();
//echo $db->sql;
//die();
//var_dump($pictures);die();
/**
 * PHPExcel
 *
 * Copyright (C) 2006 - 2012 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPExcel
 * @package    PHPExcel
 * @copyright  Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt	LGPL
 * @version    1.7.7, 2012-05-19
 */

/** Error reporting */
error_reporting(E_ALL);

//date_default_timezone_set('Europe/London');

/** Include PHPExcel */
require_once 'Classes/PHPExcel.php';


// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("chen yinyi")
							 ->setLastModifiedBy("chen yiyi")
							 ->setTitle("cyy Office 2003 XLS Test Document")
							 ->setSubject("cyy Office 2007 XLS Test Document")
							 ->setDescription(APP)
							 ->setKeywords(WHAT)
							 ->setCategory(WHAT."统计");

// Add some data
if($table == 'fanhuan_content'){
	// Add some data
	$objPHPExcel->setActiveSheetIndex(0)
	            ->setCellValue('A1', '路径')
	            ->setCellValue('B1', '标题')
	            ->setCellValue('C1', '描述')
	            ->setCellValue('D1', '图片')
	            ->setCellValue('E1', '日期')
	            ->setCellValue('F1', '原价')
	            ->setCellValue('G1', '现价')
				->setCellValue('H1', '责任编辑')
				->setCellValue('I1', '分类')
				->setCellValue('J1', '佣金')
				->setCellValue('K1', '商品id');
	// Miscellaneous glyphs, UTF-8
	foreach ($pictures as $key => $item){
		$objPHPExcel->setActiveSheetIndex(0)
					->setCellValue('A'.($key+2), $item['goodsUrl'])
					->setCellValue('B'.($key+2), $item['title'])
					->setCellValue('C'.($key+2), $item['desc'])
					->setCellValue('D'.($key+2), $item['picture'])
					->setCellValue('E'.($key+2), $item['publishDate'])
					->setCellValue('F'.($key+2), $item['originalPrice'])
					->setCellValue('G'.($key+2), $item['currentPrice'])
					->setCellValue('H'.($key+2), isset($item['nickName'])?$item['nickName']:$item['updater'])
					->setCellValue('I'.($key+2), $item['cate'])
					->setCellValue('J'.($key+2), $item['commission'])
					->setCellValue('K'.($key+2), $item['goodsID']);
	}
}else{
	// Add some data
	$objPHPExcel->setActiveSheetIndex(0)
				->setCellValue('A1', '路径')
				->setCellValue('B1', '标题')
				->setCellValue('C1', '描述')
				->setCellValue('D1', '图片')
				->setCellValue('E1', '日期')
				->setCellValue('F1', '原价')
				->setCellValue('G1', '现价')
				->setCellValue('H1', '入手')
				->setCellValue('I1', '收藏')
				->setCellValue('J1', '责任编辑')
				->setCellValue('K1', '分类')
				->setCellValue('L1', '佣金')
				->setCellValue('M1', '赞')
				->setCellValue('N1', '商品id')
				->setCellValue('O1', '点击');
	// Miscellaneous glyphs, UTF-8
	foreach ($pictures as $key => $item){
		$objPHPExcel->setActiveSheetIndex(0)
					->setCellValue('A'.($key+2), $item['goodsUrl'])
					->setCellValue('B'.($key+2), $item['title'])
					->setCellValue('C'.($key+2), $item['desc'])
					->setCellValue('D'.($key+2), $item['picture'])
					->setCellValue('E'.($key+2), $item['publishDate'])
					->setCellValue('F'.($key+2), $item['originalPrice'])
					->setCellValue('G'.($key+2), $item['currentPrice'])
					->setCellValue('H'.($key+2), $item['shared'])
					->setCellValue('I'.($key+2), $item['collected'])
					->setCellValue('J'.($key+2), isset($item['nickName'])?$item['nickName']:$item['updater'])
					->setCellValue('K'.($key+2), $item['cate'])
					->setCellValue('L'.($key+2), $item['commission'])
					->setCellValue('M'.($key+2), $item['saygood'])
					->setCellValue('N'.($key+2), "'".$item['goodsID'])
					->setCellValue('O'.($key+2), $item['clickNum']);
	}
}			


//设置单元格
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('M')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('O')->setAutoSize(true);
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle(APP);
//set font 
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('G1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('H1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('I1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('J1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('K1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('L1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('M1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('O1')->getFont()->setBold(true);

$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(15);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(15);
$objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(15);
$objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(15);
$objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(15);
$objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(15);
$objPHPExcel->getActiveSheet()->getStyle('G1')->getFont()->setSize(15);
$objPHPExcel->getActiveSheet()->getStyle('H1')->getFont()->setSize(15);
$objPHPExcel->getActiveSheet()->getStyle('I1')->getFont()->setSize(15);
$objPHPExcel->getActiveSheet()->getStyle('J1')->getFont()->setSize(15);
$objPHPExcel->getActiveSheet()->getStyle('K1')->getFont()->setSize(15);
$objPHPExcel->getActiveSheet()->getStyle('L1')->getFont()->setSize(15);
$objPHPExcel->getActiveSheet()->getStyle('M1')->getFont()->setSize(15);
$objPHPExcel->getActiveSheet()->getStyle('O1')->getFont()->setSize(15);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.WHAT.date('Y-m-d').'.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
